package cn.sfturing.web;

import cn.sfturing.entity.*;
import cn.sfturing.service.CommonUserService;
import cn.sfturing.service.DoctorService;
import cn.sfturing.service.HospitalService;
import cn.sfturing.service.OrderRecordsService;
import cn.sfturing.utils.DateUtil;
import cn.sfturing.utils.PageUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "uni")
public class UniContorller {
    @Autowired
    private OrderRecordsService orderRecordsService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private CommonUserService commonUserService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private PageUtils pageUtils;

    Map<String, Object> msgMap = new HashMap<>();

    @RequestMapping(value = "query")
    @ResponseBody
    public List<OrderRecords> query(@RequestBody JSONObject jsonparam) {

        Integer UserId = Integer.parseInt(jsonparam.get("userid").toString());


        List<OrderRecords> orderRecords = orderRecordsService.findOrderRecordsByUserID(UserId);

        return orderRecords;


    }

    @RequestMapping(value = "select")

    public @ResponseBody
    List<Doctor> select(@RequestBody JSONObject jsonparam) {
        List<Doctor> allDoctor = doctorService.findAllDoctor();
        System.out.println(allDoctor);


        //List<OrderRecords> orderRecords = orderRecordsService.findOrderRecordsByUserID( UserId);

        return allDoctor;



    }
    @RequestMapping(value = "gethos")

    public @ResponseBody
    List<Hospital> find1() {

        List<Hospital> allHos =hospitalService.gethos();
        System.out.println("lllll");
        return allHos;

    }
    @RequestMapping(value = "getdotbyname")

    public @ResponseBody
    List<Doctor> get(@RequestBody JSONObject jsonparam) {
//        String  hospitalName = jsonparam.get("hospitalName").toString();

        List<Doctor> allDot =doctorService.findDoctorByName(jsonparam.get("hospitalName").toString());
        System.out.println(allDot);
        return allDot;
    }
    @RequestMapping(value = "getdotbyid")

    public @ResponseBody Doctor get1(@RequestBody JSONObject jsonparam) {

    Integer id = Integer.parseInt(jsonparam.get("dotid").toString());

     Doctor doctor1 =doctorService.findDoctorById(id);
        System.out.println(doctor1);

     return doctor1;
    }
    @RequestMapping(value = "finduser")

    public @ResponseBody
    List<CommonUser> find() {
        List<CommonUser> commonUsers = commonUserService.findcom();
        System.out.println(commonUsers);


        //List<OrderRecords> orderRecords = orderRecordsService.findOrderRecordsByUserID( UserId);

        return commonUsers;
    }//查找所有订单记录
    @RequestMapping(value = "findord")

    public
    String find1(Model model) {
        List<OrderRecords> orderRecords = orderRecordsService.findAllById();
        System.out.println(orderRecords);
        model.addAttribute("orderRecords", orderRecords);
        return "userCenter/user";
    }
    @RequestMapping(value = "/alldingdan/{page}")
    public String allHos(Model model, @PathVariable("page") int page) {

        // 设置页面
        pageUtils.setCurrentPage(page);
        pageUtils.setTotalRecord(orderRecordsService.findNum());
        int start;
        if (pageUtils.getCurrentPage() == 0) {
            start = 0;
        } else {
            start = pageUtils.getPageRecord() * (pageUtils.getCurrentPage() - 1);
        }

        List<OrderRecords> orderRecords1 = orderRecordsService.findordbypage(start,
                pageUtils.getPageRecord());
        model.addAttribute("pages", pageUtils);
        model.addAttribute("orderRecords", orderRecords1);
        return "userCenter/user";
    }

    //查找所有医院记录
//    @RequestMapping(value = "sethos")
//    public String find3(Model model) {
//        List<Hospital> hospitals = hospitalService.findOpenHos();
//        model.addAttribute("hospitals", hospitals);
//        return "hospital/sethos";
//    }
    @RequestMapping(value = "sethos/{page}")
    public String find3(Model model, @PathVariable("page") int page) {
        // 设置页面
        pageUtils.setCurrentPage(page);
        pageUtils.setTotalRecord(hospitalService.findAllHosNum1());
        int start;
        if (pageUtils.getCurrentPage() == 0) {
            start = 0;
        } else {
            start = pageUtils.getPageRecord() * (pageUtils.getCurrentPage() - 1);
        }

        List<Hospital> hospitals =hospitalService.findAllHosByConditon1(start, pageUtils.getPageRecord());
        model.addAttribute("hospitals", hospitals);
        model.addAttribute("pages", pageUtils);
        return "hospital/sethos";
    }
    @RequestMapping(value = "addhos")

    public
    String find5() {

        return "hospital/addhos";
    }

    @RequestMapping(value = "findcom")
    String find2(Model model) {
        List<CommonUser> commonUsers =commonUserService.findcom();
        System.out.println(commonUsers);
        model.addAttribute("commonUsers", commonUsers);
        return "userCenter/usercom";
    }
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String find(@RequestBody Map<String, Object> params){
        System.out.println(params.get("id"));

        int id = Integer.parseInt(String.valueOf(params.get("id")));
        int userid = Integer.parseInt(String.valueOf(params.get("userid")));
        String time= String.valueOf(params.get("time"));
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String nowDate=format.format(date);
        System.out.println(nowDate);


        Doctor doctor = doctorService.findDoctorById(id);


        OrderRecords orderRecords = new OrderRecords();
        // 设置orderRecords
         orderRecords.setUserID(userid);
         orderRecords.setTransactTime(String.valueOf(time));
         orderRecords.setHospitalName(doctor.getHospitalName());
         orderRecords.setOfficesName(doctor.getOfficesName());
        orderRecords.setTransactDate(nowDate);
         orderRecords.setDoctorName(doctor.getDoctorName());

        // 设置预约识别码 0代表只选择时间段，未提交订单
        orderRecords.setOrderVer(1);

        //


        // 插入订单
        orderRecordsService.insertOrderRecords(orderRecords);


        //System.out.println(id);
        return "order/orderInfo";
    }




    @RequestMapping(value = "update")
    public String updateOrderd(Model model, @RequestParam(value = "id") int id,HttpSession session) {
        // 通过id更改订单状态
        System.out.println(id);
        orderRecordsService.updateOrderd(id);

        return "/userCenter/user";
    }
    @RequestMapping(value = "updatehos" )
    public String updatehos(Model model, @RequestParam(value = "id") int id, HttpSession session ) {
        // 通过id更改订单状态
        Hospital hospital=hospitalService.findHosById(id);
        model.addAttribute("hospital1", hospital);

        return "/hospital/updatehos";
    }
    @RequestMapping(value = "updateuser" )
    public String updateuser(Model model, @RequestParam(value = "userId") int userId, HttpSession session ) {

        CommonUser commonUser = commonUserService.findCommonUserByUserId(userId);
        model.addAttribute("commonUser", commonUser);

        return "/user/updateuser";
    }
    @RequestMapping(value = "updateuser1" , method = RequestMethod.POST)
    public String updateuser1(  HttpServletRequest request ) {
        System.out.println(request.getParameter("userId"));
        System.out.println("555555");

        String userIdenf = request.getParameter("userIdenf");
        String userName = request.getParameter("userName");
        String userSex = request.getParameter("userSex");
        String userEmail = request.getParameter("userEmail");
        String userMobile = request.getParameter("userMobile");
        int userId = Integer.parseInt(String.valueOf(request.getParameter("userId")));
        System.out.println("444444444");
        Hospital hospital2 = new Hospital();
        CommonUser commonUser =new CommonUser();
        commonUser.setUserEmail(userEmail);
        commonUser.setUserId(userId);
        commonUser.setUserSex(userSex);
        commonUser.setUserName(userName);
        commonUser.setUserMobile(userMobile);
        commonUser.setUserIdenf(userIdenf);
        commonUserService.updatauser(commonUser);


        return "/userCenter/usercom";
    }

    @RequestMapping(value = "updatehos1" , method = RequestMethod.POST)
    public String updatehos1(  HttpServletRequest request ) {
        System.out.println(request.getParameter("id"));
        System.out.println("555555");
        // 通过id更改订单状态
//        Hospital hospital=hospitalService.findHosById(id);
//        model.addAttribute("hospital1", hospital);
        String hospitalName = request.getParameter("hospitalName");
        String hospitalArea = request.getParameter("hospitalArea");
        String hospitalImg = request.getParameter("hospitalImg");
        String hospitalDeanName = request.getParameter("hospitalDeanName");
        String hospitalYear = request.getParameter("hospitalYear");
        String hospitalNature = request.getParameter("hospitalNature");
        String hospitalGrade = request.getParameter("hospitalGrade");
        String hospitalEquipment = request.getParameter("hospitalEquipment");
        String hospitalAbout = request.getParameter("hospitalAbout");
        String hospitalHonor = request.getParameter("hospitalHonor");
        String hospitalUrl = request.getParameter("hospitalUrl");
        String hospitalAddress = request.getParameter("hospitalAddress");
        int id = Integer.parseInt(String.valueOf(request.getParameter("id")));
        System.out.println("444444444");
        Hospital hospital2 = new Hospital();
        hospital2.setHospitalName(hospitalName);
        hospital2.setHospitalEquipment(hospitalEquipment);
        hospital2.setHospitalArea(hospitalArea);
        hospital2.setHospitalAddress(hospitalAddress);
        hospital2.setIsOpen(1);
        hospital2.setHospitalAbout(hospitalAbout);
        hospital2.setHospitalImg(hospitalImg);
        hospital2.setHospitalGrade(hospitalGrade);
        hospital2.setHospitalDeanName(hospitalDeanName);
        hospital2.setHospitalYear(hospitalYear);
        hospital2.setHospitalNature(hospitalNature);
        hospital2.setHospitalUrl(hospitalUrl);
        hospital2.setHospitalHonor(hospitalHonor);
        hospital2.setIsMedicalInsurance("医保");
        hospital2.setId(id);
        hospitalService.updatahos(hospital2);


        return "/hospital/updatehos";
    }

    @RequestMapping(value = "deletehos")
    public String deletehos( @RequestParam(value = "id") int id,HttpSession session) {
        // 通过id更改订单状态
        System.out.println(id);
        hospitalService.deletehos(id);

        return "/hospital/sethos";
    }
        @RequestMapping(value = "getcount")
        public String getcountMap(Model model) {
            List<OrderRecords> orderTimeList= orderRecordsService.findOrderCount();
            //获取x轴需要数据
            List<Object> timeList = orderTimeList.stream().map(OrderRecords::getTransactDate).collect(Collectors.toList());
            List<Object> countList = orderTimeList.stream().map(OrderRecords::getCount).collect(Collectors.toList());

            Map<String,Object> map =new HashMap<>();
            map.put("timeList",timeList);
            map.put("countList",countList);
            model.addAttribute("test",timeList);
            model.addAttribute("test1",countList);
            System.out.println(timeList);
            System.out.println(countList);

             return "/userCenter/e2";
        }
        @ResponseBody
    @RequestMapping(value = "getcount1")
    public String getcountMap1(Model model) {

        List<OrderRecords> list=orderRecordsService.findOrderCount1();

        String json= JSON.toJSONString(list);

        System.out.println(json);

        return json;
    }
    @RequestMapping(value = "adddot")
    public String adddot(Model model) {


        return "userCenter/adddot";
    }


}


